package com.jackrain.nea.fc.dl.controller;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.TransferReloadSubmitCmd;
import com.jackrain.nea.fc.dl.api.TransferReloadUnsubmitCmd;
import com.jackrain.nea.fc.dl.api.TransferReloadVoidCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

@Slf4j
@Api(description = "transferreload")
@RestController
public class TranSferReloadCtrl {

    @ApiOperation(value = "submit-查询")
    @RequestMapping(path = "/fc/api/transfer/submit", method = RequestMethod.POST)
    public JSONObject submit(HttpServletRequest request,
                              @RequestParam(value = "param", required = false) String param) throws Exception {



        log.debug("-------------------调拨出库单作废补偿--------------------"+param);

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.TransferReloadSubmitCmd",
                    "dl", "1.0");
            result = ((TransferReloadSubmitCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }


    @ApiOperation(value = "void-查询")
    @RequestMapping(path = "/fc/api/transfer/void", method = RequestMethod.POST)
    public JSONObject void1(HttpServletRequest request,
                               @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.TransferReloadVoidCmd",
                    "dl", "1.0");
            result = ((TransferReloadVoidCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }

    @ApiOperation(value = "submit-查询")
    @RequestMapping(path = "/fc/api/transfer/unsubmit", method = RequestMethod.POST)
    public JSONObject unsubmit(HttpServletRequest request,
                             @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.TransferReloadUnsubmitCmd",
                    "dl", "1.0");
            result = ((TransferReloadUnsubmitCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }


}
